//
//  ClassInformationViewController.m
//  UI_A项目
//
//  Created by dlios on 15-1-17.
//  Copyright (c) 2015年 songCheng. All rights reserved.
//

#import "ClassInformationViewController.h"

@interface ClassInformationViewController ()

@end

@implementation ClassInformationViewController

- (void)dealloc
{
    [_segment release];
    [_informationArray release];
    [_informationTableView release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *imagev = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"u=213802053,255796087&fm=21&gp=0.jpg"]];
    imagev.frame = CGRectMake(0, 270, 375, 500);
    [self.view addSubview:imagev];
    [imagev release];
    
    self.navigationItem.title = self.model.title;
    
   
    
    self.informationTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 270, 375, 347) style:UITableViewStylePlain];
    self.informationTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.informationTableView];
    [_informationTableView release];
    self.informationTableView.alpha = 0.6;
    self.informationTableView.delegate = self;
    self.informationTableView.dataSource = self;
    
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 270, 375, 500)];
    self.scrollView.hidden = YES;
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
   
    
    
    self.intro = [[introView alloc] initWithFrame:CGRectMake(0, 0, 375, 500)];
    self.intro.hidden = YES;
    self.intro.alpha = 0.6;
    
    [self.scrollView addSubview:self.intro];
    [self.intro release];
    


    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"列表", @"简介", nil];
    
    self.segment = [[UISegmentedControl alloc] initWithItems:array];
    self.segment.frame = CGRectMake(0, 160, 375, 50);
    [self.segment setTintColor:[UIColor whiteColor]];
    
    self.segment.selectedSegmentIndex = 0;
    [self.segment addTarget:self action:@selector(segment:) forControlEvents:UIControlEventValueChanged];
    [_segment release];

     [self AFN];
    
   
    
    
    
}

- (void)AFN
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/15", self.model.aoId];
//    NSLog(@"self.model = %@ ,  ~~~~~%@",self.model, self.model.aoId);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", nil];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//        NSLog(@"sssssss%@", responseObject);
        NSMutableDictionary *classDic = responseObject;
        
        NSMutableDictionary *tracksDic = [classDic objectForKey:@"tracks"];
        
        self.informationArray = [NSMutableArray array];
        
        NSMutableArray *listArray = [tracksDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in listArray) {
            ClassModel *model = [[ClassModel alloc] init];
            
            model.nickname = [dic objectForKey:@"nickname"];
            
            model.createdAt = [dic objectForKey:@"createdAt"];
            model.smallLogo = [dic objectForKey:@"smallLogo"];
            model.title = [dic objectForKey:@"title"];
            model.duration = [dic objectForKey:@"duration"];
            model.playUrl32 = [dic objectForKey:@"playUrl32"];
            model.playUrl64 = [dic objectForKey:@"playUrl64"];
            NSString *str = [NSString stringWithFormat:@"%@", [dic objectForKey:@"playtimes"]];
            model.playTimes = str;
            NSString *str1 = [NSString stringWithFormat:@"%@", [dic objectForKey:@"comments"]];
            model.comments = str1;
//            NSLog(@"oooooo = %@",model.playTimes);
            NSString *str2 = [NSString stringWithFormat:@"%@", [dic objectForKey:@"likes"]];
            model.likes = str2;
            model.albumImage = [dic objectForKey:@"albumImage"];
            model.coverLarge = [dic objectForKey:@"coverLarge"];
            model.uid = [NSString stringWithFormat:@"%@", [dic objectForKey:@"uid"]];
            
            model.orderNum = [NSString stringWithFormat:@"%@", [dic objectForKey:@"orderNum"]];
            model.downloadUrl = [dic objectForKey:@"downloadUrl"];
            
            
            /*********  *********/
            ClassInformationView *classView = [[ClassInformationView alloc] initWithFrame:CGRectMake(0, 64, 375, 210)];
            classView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"u=1982110230,2432238905&fm=21&gp=0" ofType:@"jpg"]]];
            classView.alpha = 0.7;
            [classView setModel:model];
            [self.view addSubview:classView];
            [classView release];
            [classView addSubview:self.segment];
            
            [self.informationArray addObject:model];
        
        }
        
        
        
        
        
//
        
        
        [self.informationTableView reloadData];
//        NSLog(@"self.array = %@", self.informationArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@", error);
    }];
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndetifier = @"reuse";
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];
    if (cell == nil) {
        cell = [[InformationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifier];
    }
    ClassModel *model = [self.informationArray objectAtIndex:indexPath.row];
    
    
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return self.informationArray.count;
    return self.informationArray.count;
}

- (void)segment:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        [self AFN];
             self.intro.hidden = YES;
        self.scrollView.hidden = YES;
            self.informationTableView.hidden = NO;
        [self.informationTableView reloadData];

     }
    else if (segment.selectedSegmentIndex == 1)
    {
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/15", self.model.aoId];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", nil];
            [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                [netWorkManager stopMonitoring];
                
                NSMutableDictionary *dicDic = responseObject;
                self.informationArray = [NSMutableArray array];
                NSMutableDictionary *dic = [dicDic objectForKey:@"album"];
                NSLog(@"album = %@", dic);
              ClassModel *model1 = [[ClassModel alloc] init];
                
                model1.nickname1 = [dic objectForKey:@"nickname"];
                
                model1.title = [dic objectForKey:@"title"];
                
                model1.tags = [dic objectForKey:@"tags"];
                
                model1.intro1 = [dic objectForKey:@"intro"];
                
                model1.downloadUrl = [dic objectForKey:@"downloadUrl"];
                
                NSLog(@"nickname = %@", model1.intro1);
                [self.intro setModel:model1];
                
                [self.informationArray addObject:model1];

                
                self.intro.hidden = NO;
                self.scrollView.hidden = NO;
                self.informationTableView.hidden = YES;
        
               [self.informationTableView reloadData];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                
                
            }];
        
        
    }
    
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
